<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=10.0"
    />
    <title>冷弋白的网页</title>
    <link
      href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css"
      rel="stylesheet"
    />
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
    <style>
      * {
        margin: 0;
        padding: 0;
      }

      body {
        display: flex;
        height: 100vh;
        flex-flow: column wrap;
        justify-content: center;
        align-items: center;
      }

      .btn {
        width: 200px;
        height: 50px;
        margin: 10px;
      }
    </style>
  </head>

  <body>
    <button type="button" class="btn btn-primary">发送请求</button>
    <button type="button" class="btn btn-warning">取消请求请求</button>
    <script>
      const btns = document.querySelectorAll('.btn');
      let cancel = null;
      //发送请求
      btns[0].addEventListener('click', function () {
        axios({
          url: 'https://autumnfish.cn/api/joke',
          cancelToken: new axios.CancelToken(c => {
            cancel = c;
          }),
        }).then(res => {
          console.log(res);
        });
      });

      //取消请求
      btns[1].addEventListener('click', function () {
        cancel();
      });
    </script>
  </body>
</html>
